package com.astrofizzbizz.raspberrypicamera.client;

import com.astrofizzbizz.raspberrypicamera.shared.RaspberryPiException;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class TakeImagePanel extends VerticalPanel
{
	private RaspberryPiCamera entryPoint;
	private Button takeImageButton = new Button("Take Image");
	private Button okButton = new Button("OK");
	HorizontalPanel delayTimePanel = new HorizontalPanel();
	private TextBox delayTimeTextBox = new TextBox();
	private int delaytime = 300;
	private int version = 1;
	private Image image;
	
	public TakeImagePanel(RaspberryPiCamera entryPoint)
	{
		super();
		this.entryPoint = entryPoint;
		delayTimePanel.add(new Label("Delay Time"));
		delayTimeTextBox.setText(Integer.toString(delaytime));
		delayTimePanel.add(delayTimeTextBox);
		delayTimePanel.add(new Label("mS"));
		add(delayTimePanel);
		add(takeImageButton);
		takeImageButton.addClickHandler(new TakeImageButtonClickHandler());
		image = new Image("cameraImages/stills/temp.jpg?v" + version);
		image.setSize("800px", "600px");
		image.setVisible(false);
		add(image);
		okButton.setVisible(false);
		okButton.addClickHandler(new OkButtonClickHandler());
		add(okButton);
	}
	private class TakeImageButtonClickHandler implements ClickHandler
	{
		@Override
		public void onClick(ClickEvent event) 
		{
			String[] debugResponse = {""};
			takeImageButton.setVisible(false);
			delayTimePanel.setVisible(false);
			delaytime = Integer.parseInt(delayTimeTextBox.getText());
			try {
				String switchSettings = entryPoint.getMyTabLayoutPanel().getCameraSettingsPanel().getSwitchSettings();
				entryPoint.getRaspberryPiService().takeSingleImage("temp.jpg", delaytime, switchSettings, entryPoint.isDebug(), debugResponse, new TakeImageCallback());
			} catch (RaspberryPiException e) 
			{
				takeImageButton.setVisible(true);
				delayTimePanel.setVisible(true);
				entryPoint.getMyTabLayoutPanel().selectTab(entryPoint.getMyTabLayoutPanel().getCameraSettingsPanelTabId());
			}
		}

	}
	private class OkButtonClickHandler implements ClickHandler
	{
		@Override
		public void onClick(ClickEvent event) 
		{
			image.setVisible(false);
			okButton.setVisible(false);
			takeImageButton.setVisible(true);
			delayTimePanel.setVisible(true);
	}

	}
	class TakeImageCallback implements AsyncCallback<String[]>
	{
		@Override
		public void onFailure(Throwable caught) 
		{
			takeImageButton.setVisible(true);
			delayTimePanel.setVisible(true);
			entryPoint.getStatusTextArea().addStatus("Error on Take Image: " + caught.getMessage());
		}
		@Override
		public void onSuccess(String[] result) 
		{
			entryPoint.getStatusTextArea().addStatus("Success on Taking Image.");
			version = version + 1;
			image.setUrl("cameraImages/stills/temp.jpg?v" + version);
			image.setVisible(true);
			okButton.setVisible(true);
			
		}
	}
}
